﻿namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class NewsRepository : INewsRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region INewsRepository Members

		public int CreateNews(Data.Entity.News theNewNews)
		{
			if(theNewNews == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_News_Create(theNewNews.LanguageID, theNewNews.Title, theNewNews.ShortContent, theNewNews.FullContent,
					theNewNews.Published, theNewNews.AllowComments, theNewNews.CreatedOn);
			}

			return result;
		}

		public IList<Data.Entity.News> GetNewsByLanguage(int languageID)
		{
			if(languageID <= 0)
			{
				return null;
			}

			List<ECommerce_GetNews_By_LanguageResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetNews_By_Language(languageID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.News>();

				foreach(var n in lstData)
				{
					var theEntity = new Data.Entity.News()
					{
						AllowComments = n.AllowComments,
						CreatedOn = n.CreatedOn,
						FullContent = n.FullContent,
						LanguageID = n.LanguageID,
						NewsID = n.NewsID,
						Published = n.Published,
						ShortContent = n.ShortContent,
						Title = n.Title
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int DeleteNews(int newsID)
		{
			if (newsID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_News_Delete(newsID);
			}

			return result;
		}

		public IList<Data.Entity.News> GetAllNewsByLanguage(int languageID)
		{
			if(languageID <= 0)
			{
				return null;
			}

			List<ECommerce_GetNews_By_LanguageResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetNews_By_Language(languageID).OrderByDescending(exp => exp.CreatedOn);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.News>();

				foreach(var n in lstData)
				{
					var theEntity = new Data.Entity.News()
					{
						AllowComments = n.AllowComments,
						CreatedOn = n.CreatedOn,
						FullContent = n.FullContent,
						LanguageID = n.LanguageID,
						NewsID = n.NewsID,
						Published = n.Published,
						ShortContent = n.ShortContent,
						Title = n.Title
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public Data.Entity.News GetTheNews(int id)
		{
			if (id <= 0)
			{
				return null;
			}

			List<Data.New> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.News.Where(exp => exp.NewsID == id);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var n = lstData[0];

				Data.Entity.News result;

				result = new Data.Entity.News()
				{
					AllowComments = n.AllowComments,
					CreatedOn = n.CreatedOn,
					FullContent = n.FullContent,
					LanguageID = n.LanguageID,
					NewsID = n.NewsID,
					Published = n.Published,
					ShortContent = n.ShortContent,
					Title = n.Title
				};

				return result;
			}

			return null;
		}

		public int UpdateNews(Data.Entity.News theNews)
		{
			if (theNews == null || theNews.NewsID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbNews = (from n in _dbContext.News
							  where n.NewsID.Equals(theNews.NewsID)
							  select n).FirstOrDefault();

				if(dbNews == null || dbNews.NewsID <= 0)
				{
					result = 0;
				}
				else
				{
					dbNews.AllowComments = theNews.AllowComments;
					dbNews.CreatedOn = theNews.CreatedOn;
					dbNews.FullContent = theNews.FullContent;
					dbNews.LanguageID = theNews.LanguageID;
					dbNews.Published = theNews.Published;
					dbNews.ShortContent = theNews.ShortContent;
					dbNews.Title = theNews.Title;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		#endregion
	}
}
